﻿using System;
using System.Diagnostics;
using System.Linq;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.Common;

namespace BringToFront
{
    public partial class BringToFront : HBPlugin
    {
        //Normal Stuff.
        public override string Name { get { return "BringToFront"; } }
        public override string Author { get { return "Bambam922"; } }
        public override Version Version { get { return new Version(1, 2, 0); } }
        public override bool WantButton { get { return false; } }
        public override string ButtonText { get { return "BringToFront"; } }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        public override void Pulse()
        {
                if (Battlegrounds.BattlegroundStatuses.Any(status => status == BattlegroundStatus.Confirm)) { FocusWindowPvp(); }
        }

        public override void Initialize()
        {
            Lua.Events.AttachEvent("LFG_PROPOSAL_SHOW", FocusWindowPve);
        }

        public static void FocusWindowPve(object sender, LuaEventArgs args)
        {

            Process[] procs = Process.GetProcessesByName("Wow");
            Logging.Write("Popped {0} To Foreground & accepted invite. ", procs[0].MainWindowTitle.ToString());
            if (procs.Length > 0)
            {
                
                // Which one?  I'll just pick the first
                if (procs[0].MainWindowHandle != IntPtr.Zero)
                    Lua.DoString("AcceptProposal()");
                    SetForegroundWindow(procs[0].MainWindowHandle);
                   
            }                     
        }

        public static void FocusWindowPvp()
        {

            Process[] procs = Process.GetProcessesByName("Wow");
            Logging.Write("Popped {0} To Foreground & accepted invite. ", procs[0].MainWindowTitle.ToString());
            if (procs.Length > 0)
            {
                
                // Which one?  I'll just pick the first
                if (procs[0].MainWindowHandle != IntPtr.Zero)
                    Lua.DoString("AcceptBattlefieldPort()");
                    SetForegroundWindow(procs[0].MainWindowHandle);

            }
        }
    }
}

